import 'package:flutter/material.dart';

class BlockRefreshWithCallback extends StatefulWidget {
  const BlockRefreshWithCallback({Key? key}) : super(key: key);

  @override
  _BlockRefreshWithCallbackState createState() =>
      _BlockRefreshWithCallbackState();
}

class _BlockRefreshWithCallbackState extends State<BlockRefreshWithCallback> {
  int currentIndex = 0;
  List<RightMenu> menus = [
    RightMenu(0),
    RightMenu(1),
    RightMenu(2),
  ];

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text("Refreshable局部刷新"),
          centerTitle: true,
        ),
        body: Container(
          child: Row(
            children: [
              Container(
                color: Colors.grey,
                width: 100,
                child: LeftMenuWidget((index) {
                  setState(() {
                    currentIndex = index;
                  });
                }),
              ),
              Expanded(child: menus[currentIndex])
            ],
          ),
        ),
      ),
    );
  }
}

class LeftMenuWidget extends StatefulWidget {
  final Function(int index) callback;

  const LeftMenuWidget(this.callback, {Key? key}) : super(key: key);

  @override
  _LeftMenuWidgetState createState() => _LeftMenuWidgetState();
}

class _LeftMenuWidgetState extends State<LeftMenuWidget> {
  List<String> list = ["菜单1", "菜单2", "菜单3"];

  @override
  Widget build(BuildContext context) {
    return Container(
      child: ListView.separated(
          itemBuilder: (context, index) {
            return ListTile(
              title: Text(
                list[index],
                style: Theme.of(context).textTheme.bodyText1,
              ),
              onTap: () {
                widget.callback.call(index);
              },
            );
          },
          separatorBuilder: (context, index) {
            return Divider();
          },
          itemCount: list.length),
    );
  }
}

class RightMenu extends StatefulWidget {
  final int index;

  const RightMenu(this.index, {Key? key}) : super(key: key);

  @override
  _RightMenuState createState() => _RightMenuState();
}

class _RightMenuState extends State<RightMenu> {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Center(
        child: Text("${widget.index}"),
      ),
    );
  }
}
